﻿@using CoralNode.Core.Entity.Domains.Seo
@using CoralNode.Core.Entity.Enums
@using CoralNode.Core.Entity.Model
@using CoralNode.MvcClient.Contexts
@using CoralNode.MvcClient.Helper
@using Coralcode.Utils.Basic
@using Coralcode.Base.Utils;
@model CoralNode.Core.Entity.Domains.Site.MenuDomain

@{
    ViewBag.Title = "Edit";
    LinkMenuModel linkMenuModel = null;
    StaticBlockMenuModel staticBlockModel = null;
}
@Html.IncludePage("CoralAdmin/prototype", "_Left", new { current = "menu" })
<div id="pbody">
    <div class="extra scrolltab">
        <div class="pbody_top">
            <span class="ico ico_setting_system"></span><span class="t t_c">菜单设计&nbsp;&nbsp;/&nbsp;&nbsp;<b
                class=" t_small f_no">编辑菜单</b></span>
        </div>
        <div id="main">
            <div class="local">
                <a href="/m/menu/listMenu?GroupId=@Model.Group.Id" class="ico ico_back btn"></a>
            </div>
            <div class="scrolltab-triggers extra_normal">
                <a href="javascript:">主要信息</a>
                <a href="javascript:">菜单配置</a>
                <a href="javascript:">SEO配置</a>
            </div>
            <form id="spform" action="/m/menu/editMenu/@Model.Id" method="post" enctype="multipart/form-data">
                @if (@ViewBag.Msg != null) {
                    <div class="error_msg">
                        @ViewBag.Msg.Msg
                    </div>
                }
                <div class="pt_1  scrolltab-items  pr_2">
                    <div class="table_form form_main ">
                        <div class="scrolltab-item-t">
                            <span class="btn"></span>主要信息
                        </div>
                        <div class="pl_2">
                            @Html.AntiForgeryToken()
                            @Html.ValidationSummary(true)
                            <table>
                                <tbody>
                                    <tr>
                                        <th width="60">菜单名称<span class="rq">*</span></th>
                                        <td>
                                            @Html.TextBoxFor(m => m.Title, new { @class = "text w_37 required" })
                                            @Html.HiddenFor(m => m.SiteId)
                                            @Html.HiddenFor(m => m.Id)
                                        </td>
                                    </tr>
                                    <tr>
                                        <th width="60">菜单路径<span class="rq">*</span></th>
                                        <td>
                                            <label>@string.Format("http://{0}/{1}/",HttpContext.Current.Request.Url.Host,CoralNodeContext.Current.Workbench.Hosts)</label>@Html.TextBoxFor(m => m.Path, new { @class = "text w_37 required", @id = "path" })
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>菜单组</th>
                                        <td>
                                            <select id="menuGroup" name="Group.Id">
                                                @foreach (var item in @ViewBag.MenuGroupList) {
                                                    if (@item.Id == @Model.Group.Id) {
                                                    <option selected="selected" value="@item.Id">@item.Title</option>
                                                    } else {
                                                    <option value="@item.Id">@item.Title</option>
                                                    }

                                                }
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>父菜单</th>
                                        <td>
                                            <div class="ztreeContainer">
                                                <input type="text" class="text w_12 required js_ztree_trigger"  id="category_title" value="@ViewBag.ParentTitle" />
                                                <input type="hidden" name="ParentId" id="parent_id" value="@Model.ParentId" />
                                                <span><a id="menuBtn" href="#" class="js_ztree_trigger">选择</a></span>
                                                <ul id="parent_list" class="ztree" style="margin-top: 0; width: 180px;"></ul>
                                                <textarea class="js_ztree_data" style="display: none;">@ViewBag.MenuData</textarea>
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>菜单状态</th>
                                        <td>
                                            @foreach (var item in @ViewBag.MenuStatusList) {
                                                <label class="mr_2">
                                                    <input name="IsValid"  class="radio" @(item.Value==Model.IsValid?"checked=checked":string.Empty) type="radio" value="@item.Value"><span>@item.Key</span></label>
                                            }
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>

                    <div class="table_form form_main">
                        <div class="scrolltab-item-t">
                            <span class="btn"></span>菜单配置
                        </div>
                        <div class="pl_2">
                            <table>
                                <tbody class="menuType">
                                    <tr class="not_remove">
                                        <th>菜单类型</th>
                                        <td>
                                            <select id="menuType" name="MenuType">
                                                @foreach (var item in @ViewBag.MenuTypeList) {
                                                    if (Model.MenuType == @item.Value) {
                                                        <option selected="selected" value="@item.Value">@item.Key</option>
                                                    } else {
                                                        <option value="@item.Value">@item.Key</option>
                                                    }
                                                }
                                            </select>
                                        </td>
                                    </tr>
                                     <tr class="not_remove">
                                        <th width="60">其他设置</th>
                                        <td>
                                            @Html.TextBoxFor(m => m.Options, new { @class = "text w_37" })
                                        </td>
                                    </tr>
                                    <tr>
                                        <th width="60">模板</th>
                                        <td>
                                            <input type="text" name="Templet" class="text w_37" value="@Model.Templet"/>
                                        </td>
                                    </tr>
                                    @switch (Model.MenuType) {
                                        case 1:
                                            <tr>
                                                <th>具体内容</th>
                                                <td>
                                                    <textarea id="content" name="content">@Model.Content</textarea>
                                                    <script type="text/javascript">
                                                        UE.getEditor("content")
                                                    </script>
                                                </td>
                                            </tr>
                                            break;
                                        case 2:
                                            <tr>
                                                <th width="60">分类</th>
                                                <td>
                                                    <div class="js_selectCategory ztreeContainer" style="position: relative;">
                                                        <input type="text" value="" class="w_10 text js_ztree_trigger" />
                                                        <input type="hidden" name="CategoryId" value="@Model.CategoryId" />
                                                        <ul class="ztree"></ul>
                                                        <textarea class="js_ztree_data" style="display: none;">@ViewBag.ZtreeCategory</textarea>
                                                    </div>
                                                </td>
                                            </tr>
                                            break;
                                        case 3:
                                            <tr>
                                                <th width="60">标签</th>
                                                <td>
                                                    <select name="TagId">
                                                        @foreach (var item in @ViewBag.TagList) {
                                                            if (Model.TagId == @item.Id) {<option selected="selected" value="@item.Id">@item.Title</option>} else {<option value="@item.Id">@item.Title</option>}
                                                        }
                                                    </select>
                                                </td>
                                            </tr>
                                            break;
                                        case 4:
                                            <tr>
                                                <th width="60">原型</th>
                                                <td>
                                                    <select name="PrototypeId">
                                                        @foreach (var item in @ViewBag.PrototypeList) {
                                                            if (Model.PrototypeId == @item.Id) {<option selected="selected" value="@item.Id">@item.Title</option>} else {<option value="@item.Id">@item.Title</option>}
                                                        }
                                                    </select>
                                                </td>
                                            </tr>
                                            break;
                                        case 5: linkMenuModel = Newtonsoft.Json.JsonConvert.DeserializeObject<LinkMenuModel>(Model.LinkUrl);
                                         
                                            <tr style="position: relative; z-index: 2;">
                                                <th width="100">
                                                    <label class="js_link_menu">
                                                        <input type="radio" name="linkMenu.LinkType" class="mt_0-5 mr_0-5" value="2" @(linkMenuModel.LinkType == LinkType.Menu ? "checked='checked'" : string.Empty)>跳转到菜单：</label>
                                                </th>
                                                <td>
                                                    <div class="ztreeContainer">
                                                        <input type="text" name="" class="text w_37" value="@(linkMenuModel.LinkType == LinkType.Menu && linkMenuModel.Menu != null ? linkMenuModel.Menu.Title : "")">
                                                        <input type="hidden" value="@(linkMenuModel.LinkType == LinkType.Menu && linkMenuModel.Menu != null ? linkMenuModel.Menu.Id : 0)" name="linkMenu.Value"><ul
                                                                                                                                                                                                                    class="ztree"></ul>
                                                    </div>
                                                </td>
                                            </tr>
                                            <tr style="position: relative; z-index: 1;">
                                                <th width="60">
                                                    <label class="js_link_url">
                                                        <input type="radio" name="linkMenu.LinkType" class="mt_0-5 mr_0-5" value="1"  @(linkMenuModel.LinkType == LinkType.Url ? "checked='checked'" : string.Empty)>跳转到网址：</label>
                                                </th>
                                                <td>
                                                    <input type="text" name="linkMenu.Value" class="text w_37" value="@(linkMenuModel.LinkType == LinkType.Url && !string.IsNullOrEmpty(linkMenuModel.Value) ? linkMenuModel.Value : "http://")">
                                                </td>
                                            </tr>
                                            break;
                                        case 6:
                                            try {
                                                staticBlockModel = Newtonsoft.Json.JsonConvert.DeserializeObject<StaticBlockMenuModel>(Model.Content);
                                            } catch (Exception) {
                                                staticBlockModel = null;
                                            }
                                                   
                                            <tr>
                                                <th width="60">内容块</th>
                                                <td>
                                                    <select name="staticBlock.BlockId">
                                                        @foreach (var item in ViewBag.BlockList) {
                                                            if (staticBlockModel != null && staticBlockModel.BlockId == item.Id) {<option selected="selected" value="@item.Id">@item.Title</option>} else {<option value="@item.Id">@item.Title</option>}
                                                        }
                                                    </select>
                                                    @foreach (var item in (Dictionary<string, object>)EnumUtil.GetDescriptionValues(NodeEnums.StaticNodeStatus)) {
                                                        <label class="ml_2">
                                                            <input class="mt_0-5" type="radio" value="@item.Value" name="staticBlock.Status" @(staticBlockModel != null && staticBlockModel.Status == Convert.ToInt32(item.Value) ? "checked=checked" : string.Empty) />@item.Key</label>
                                                    }
                                                </td>
                                            </tr>
                                            break;

                                    }
                                </tbody>
                            </table>
                        </div>
                    </div>

                    <div class="table_form form_main">
                        <div class="scrolltab-item-t">
                            <span class="btn"></span>SEO配置
                        </div>
                        <div class="pl_2">
                            <table>
                                @{
                                    var seo = ViewBag.MenuSeo ?? new SeoMetaDomain();
                                }
                                <tbody>
                                    <tr>
                                        <th width="60">页面标题</th>
                                        <td>
                                            <input type="text" class="text w_37" name="seo.Title" value="@seo.Title"/>
                                            <input type="hidden" value="@seo.Id" name="seo.Id"/>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th width="60">页面关键字</th>
                                        <td>
                                            <input type="text" class="text w_37" name="seo.Keyword" value="@seo.Keyword"/>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th width="60">页面描述</th>
                                        <td>
                                            <textarea name="seo.Description" class="w_58 h_10">@seo.Description</textarea>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <div class="submitbox pl_2 pr_2 pt_1 pb_1 extra_normal">
                    <input type="submit" class="btn_1" value="提    交" />
                    <input type="hidden" id="ueHidden" value='</script>' />
                </div>
            </form>
        </div>
    </div>
</div>
<div id="categoryMenu_data">@ViewBag.ZtreeCategory</div>
<script type="text/javascript">



    $(document).ready(function () {
        if ($(".js_selectCategory").length > 0)
            initCategroyTree($(".js_selectCategory"));

        function initCategroyTree($parent) {
            $parent.find("ul").zTreeInit({
                data: $("#categoryMenu_data").text(), trigger: $parent.find("input[type='text']"), checkFun: function (treeNode) {
                    var showText = treeNode.name;
                    var tempNode = treeNode;
                    while (tempNode.getParentNode() != null) {
                        tempNode = tempNode.getParentNode();
                        showText = tempNode.name + " > " + showText;
                    }
                    $parent.find("input[type='text']").val(showText);
                    $parent.find("input[type='hidden']").val(treeNode.id);
                }
            });
        }

        $('#menuType').live("change", function () {
            $('.menuType tr:not(.not_remove)').remove();
            var type = $('#menuType option:selected').val();
            switch (type) {
                case "1":
                    var id = Math.random();
                    $('.menuType').append('<tr><th width="60">模板</th><td><input type="text" name="Templet" value="default" class="text w_37"/></td></tr><tr><th>具体内容</th><td><textarea id="' + id + '" name="content"></textarea><script type="text/javascript">UE.getEditor("' + id + '")' + $('#ueHidden').val() + '</td></tr>');
                    break;
                case "5":
                    $('.menuType').append('<tr  style="position:relative; z-index:2;"><th width="100"><label class="js_link_menu"><input type="radio" name="linkMenu.LinkType" class="mt_0-5 mr_0-5" value="2" />跳转到菜单：</label></th><td><div class="ztreeContainer"><input type="text" name="" class="text w_37" /><input type="hidden" value="" name="linkMenu.Value" /><ul class="ztree"></ul></div></td></tr><tr  style="position:relative; z-index:1;"><th width="60"><label class="js_link_url"><input type="radio" name="linkMenu.LinkType" class="mt_0-5 mr_0-5" value="1" />跳转到网址：</label></th><td><input type="text" name="linkMenu.Value" class="text w_37" value="http://" /></td></tr>'); linkMenu();
                    break;
                case "2":
                    //var html = $('.js-categoryId').html();
                    $('.menuType').append('<tr><th width="60">模板</th><td><input type="text" name="Templet" value="default" class="text w_37"/></td></tr><tr><th width="60">分类</th><td><div class="ztreeContainer js_selectCategory "><input type="text" value="" class="w_10 text" /><input type="hidden" name="CategoryId"  /><ul class="ztree"></ul></div></td></tr>'); initCategroyTree($(".js_selectCategory"));
                    break;
                case "3":
                    $('.menuType').append('<tr><th width="60">模板</th><td><input type="text" name="Templet" value="default" class="text w_37"/></td></tr><tr><th width="60">标签</th><td><select name="TagId">@foreach (var item in @ViewBag.TagList) {
                                                                                                                                                                                                               if (Model.TagId == @item.Id) {<option selected="selected" value="@item.Id">@item.Title</option>} else {<option value="@item.Id">@item.Title</option>}
                                                                                                                                                                                                           }</select></td></tr>');
                    break;
                case "4":
                    $('.menuType').append('<tr><th width="60">模板</th><td><input type="text" name="Templet" value="default" class="text w_37"/></td></tr><tr><th width="60">原型</th><td><select name="PrototypeId">@foreach (var item in @ViewBag.PrototypeList) {
                                                                                                                                                                                                                     if (Model.PrototypeId == @item.Id) {<option selected="selected" value="@item.Id">@item.Title</option>} else {<option value="@item.Id">@item.Title</option>}
                                                                                                                                                                                                                 }</select></td></tr>');
                    break;
                case "6":
                    $('.menuType').append('<tr><th width="60">模板</th><td><input type="text" name="Templet" value="default" class="text w_37"/></td></tr><tr><th width="60">内容块</th><td><select name="staticBlock.BlockId">@foreach (var item in ViewBag.BlockList) {<option @(staticBlockModel != null && staticBlockModel.BlockId == item.Id ? "selected=selected" : string.Empty) value="@item.Id">@item.Title</option>}</select><div class="pl_2">@foreach (var item in (Dictionary<string, object>)EnumUtil.GetDescriptionValues(NodeEnums.StaticNodeStatus)) {<label class="ml_2"><input class="mt_0-5" type="radio" value="@item.Value" name="staticBlock.Status" @(staticBlockModel != null && staticBlockModel.Status == Convert.ToInt32(item.Value) ? "checked=checked" : string.Empty) />@item.Key</label>}</div></td></tr>');
                default:
            }
        });

        $('#menuGroup').change(function () {
            var groupId = $('#menuGroup option:selected').val();
            $('#category_title').val("根目录");
            $('#parent_id').val(0);
            $.post("/m/menu/GetMenuList", { groupId: groupId, parentId: "@Model.ParentId", menuId: "@Model.Id" }, function (data) {
                $("#parent_list").data("zTree").setData(data);
            });
        });
        linkMenu();
        function linkMenu() {
            $(".js_link_menu,.js_link_url").click(function (e) {
                var $this = $(this),
                    $radio = $this.find("input");
                if ($radio.is(":checked")) {
                    $this.parents("tr").siblings("tr").find("th").find(".js_link_menu,.js_link_url").parent("th").siblings("td").find("input[type='text'],input[type='hidden']").attr("disabled", "disabled");
                    $this.parents("th").siblings("td").find("input[type='text']").removeAttr("disabled");
                }
            });
            $(".js_link_menu").bind("click.trigger", function (e) {
                var $this = $(this);
                $this.unbind("click.trigger");
                $.post("/m/menu/GetCurrentMenuList/", { menuId: "@(linkMenuModel != null && linkMenuModel.Menu != null ? linkMenuModel.Menu.Id : 0)" }, function (data) {
                    var $ztreeContainer = $this.parents("th").siblings("td").find(".ztreeContainer");
                    $ztreeContainer.find("ul").zTreeInit({
                        data: data, trigger: $ztreeContainer.find("input[type='text']"), checkFun: function (treeNode) {
                            var showText = treeNode.name;
                            var tempNode = treeNode;
                            while (tempNode.getParentNode() != null) {
                                tempNode = tempNode.getParentNode();
                                showText = tempNode.name + " > " + showText;
                            }

                            $ztreeContainer.find("input[type='text']").val(showText);
                            $ztreeContainer.find("input[type='hidden']").val(treeNode.id);
                        }
                    }).data("zTree").showZtree();
                });

            });
            var $ul = $(".js_link_menu").parents("tr").find("td ul"),
                $hidden = $ul.parent().prev(),
                $text = $hidden.prev();
            $(".js_link_menu,.js_link_url").find("input[type='radio']:checked").parent().trigger("click");
            $("body").trigger("click");
        }
    });
</script>
